package com.mxy.myapplication2.kotlin

import android.os.Bundle
import android.os.Handler
import android.os.Message
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.LinearLayoutManager
import com.mxy.myapplication2.R
import kotlinx.android.synthetic.main.activity_firstkotlinactivity.*
import java.lang.ref.WeakReference
import java.text.SimpleDateFormat
import java.util.*
import kotlin.collections.ArrayList

/**
 * kotlin初体验
 */
class MyFirstKotlinActivity : AppCompatActivity(), MyInterface {

    override val number1: Int = 0

    override var number2: Double = 0.5


    companion object {

        lateinit var myHandler: MyHandler

        class MyHandler constructor(val weakActivity: WeakReference<MyFirstKotlinActivity>) : Handler() {

            override fun handleMessage(msg: Message?) {
                weakActivity.get()?.let {
                    val activity: MyFirstKotlinActivity? = it
                    activity?.myHandleMessage(msg)
                }
            }
        }
    }

    /**
     * 自定义消息处理方法
     * */
    fun myHandleMessage(msg: Message?) {
        msg?.let {
            println("hashMap.size: " + hashMap.size)
        }
    }


    private val linearLayoutManager: LinearLayoutManager by lazy {
        LinearLayoutManager(this)
    }
    private val hashMap: HashMap<String, Any> by lazy {
        HashMap<String, Any>()
    }
    private val simpleDateFormat: SimpleDateFormat by lazy {
        SimpleDateFormat("yyyy-MM-dd")
    }
    private lateinit var adapterForKotlinRv: AdapterForKotlinRv
    private val arrayList: ArrayList<Int> = ArrayList<Int>()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        println("FirstActivity  getTaskId:" + taskId)
        myHandler = MyHandler(WeakReference(this))
        setContentView(R.layout.activity_firstkotlinactivity)
        initView()
    }


    /**
     * 初始化控件
     */
    private fun initView() {
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)
        arrayList.add(0)

        this.recyclerView.layoutManager = this.linearLayoutManager
        this.adapterForKotlinRv = AdapterForKotlinRv(this, arrayList)
        this.recyclerView.adapter = this.adapterForKotlinRv

        //这是常规创建一个线程的写法
        object : Thread(object : Runnable {
            override fun run() {
                myHandler.sendEmptyMessage(0)
            }
        }) {}.start()

        //这是使用lambda表达式的写法
//        object : Thread({ myHandler.sendEmptyMessage(0) }) {}.start()

        val arrayList: ArrayList<Int> = java.util.ArrayList()
        arrayList.add(1)
        arrayList.add(2)
        arrayList.add(3)

        //将函数作为参数传入
        println("集合累加: " + getValue(arrayList, ::countArrayList))

        val lambda = { array: ArrayList<Int> ->
            array.add(0)
            array.add(0)
            array.add(0)
            array.add(0)
            array.add(0)
            println("集合数量: " + array.size)
            array.size
        }

        lambda.invoke(arrayList)

        val number1: Int = 1
        val number2 = if (number1 > 0) 100 else -1//三元表达式

        //将lambda表达式作为参数传入
        getValue(arrayList, lambda)
    }

    /**
     * 这是kotlin语法中使用并发控制的方法1
     * 使用 @Synchronized 关键字
     **/
    private @Synchronized
    fun getValue(arrayList: ArrayList<Int>, body: (ArrayList<Int>) -> Int): Int {
        return body(arrayList)
    }


    /**
     * 这是kotlin语法中使用并发控制的方法2
     * 方法无并发限制，但是在方法体内有并发块
     * */
    private fun getValue2(arrayList: ArrayList<Int>, add: (ArrayList<Int>) -> Int): Int {
        //方法体内的并发控制块，将参数作为锁传进来
        synchronized(arrayList) {
            return add(arrayList)
        }
    }


    private fun countArrayList(arrayList: ArrayList<Int>): Int {
        var count = 0
        for (i: Int in arrayList) {
            count += i
        }
        return count
    }

    /**
     * apply函数  赋值
     **/
    private fun applyPlusletAssignment(giveMeANumber: Int) {

        val size: Int = arrayList.apply {
            arrayList.add(0)
        }.let { arrayList.size }
    }


    /**
     * apply函数  返回
     * */
    private fun applyPlusletReturn(): Int {

        arrayList.apply {
            arrayList.add(0)
        }.let { return arrayList.size }
    }


    /**
     * apply+let函数  赋值
     * apply函数可以对象本身传入{}中，可以通过this调用
     **/
    private fun runPlusletAssignment() {

        val size: Int = arrayList.apply {
            this.add(0)
        }.let { arrayList.size }
    }


    /**
     * run+let函数  返回
     * run函数将对象传入{}中，可以通过this调用,返回最后一行的对象
     * */
    private fun runPlusletReturn() {

        val bcd: Int = arrayList.run {
            this.add(0)
            10
        }
        //bcd的值是10
        println(bcd)
    }
}